package org.codeart.juc.visible;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {

    public static boolean flag = true;

    public static final Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            while (flag) {
                lock.lock();
                try {
                    // ...
                } finally {
                    lock.unlock();
                }
            }
            System.out.println("t1 thread ends");
        }, "t1").start();
        Thread.sleep(100);
        flag = false;
        System.out.println("main thread sets flag false");
    }
}
